package net.ipinglun.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.andexert.library.RippleView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import net.ipinglun.R;
import net.ipinglun.activitys.NewsDetailActivity;
import net.ipinglun.models.NewsBean;
import net.ipinglun.utils.Tools;

import java.util.ArrayList;

public class NewsWaterFallListViewAdapter extends BaseAdapter {



    private Context mContext;
    private int mChannelId;
    private int mImageWidth;
    private int mLastCount;
    private ArrayList<NewsBean> mItems = new ArrayList<NewsBean>();

    private DisplayImageOptions mOptions;
    private DisplayImageOptions mOptionsFadeIn;


    public NewsWaterFallListViewAdapter(Context context, int channelId) {
        mContext = context;
        mChannelId = channelId;

        mImageWidth = (Tools.getDisplayMetrics((Activity) mContext).widthPixels - (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, mContext.getResources().getDisplayMetrics()))/2;

        mOptions = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
                .showImageOnLoading(R.drawable.placeholder_image_200_150)
                .showImageOnFail(R.drawable.placeholder_image_200_150)
                .showImageForEmptyUri(R.drawable.placeholder_image_200_150)
                        //.displayer(new FadeInBitmapDisplayer(500))
                        //.displayer(new RoundedBitmapDisplayer(0))
                .build();

        mOptionsFadeIn = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
                .showImageOnLoading(R.drawable.placeholder_image_200_150)
                .showImageOnFail(R.drawable.placeholder_image_200_150)
                .showImageForEmptyUri(R.drawable.placeholder_image_200_150)
                .displayer(new FadeInBitmapDisplayer(300))
                .build();

    }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;




        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.waterfall_item, null);
            holder.news_title = (TextView) convertView.findViewById(R.id.news_title);
            holder.news_comment = (TextView) convertView.findViewById(R.id.news_comment1);
            holder.news_img = (ImageView) convertView.findViewById(R.id.news_img);
            holder.ripple_view = (RippleView) convertView.findViewById(R.id.ripple_view);

            LayoutParams params = holder.news_img.getLayoutParams();
            params.height = mImageWidth * 3/4;
            params.width = mImageWidth;
            holder.news_img.setLayoutParams(params);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String imageUrl = mItems.get(position).image_url.replaceAll(".jpg", ",c_fill,h_" + 150 + ",w_" + 200 + ".jpg");

        if (position > mItems.size() - mLastCount) {
            ImageLoader.getInstance().displayImage(imageUrl, holder.news_img, mOptionsFadeIn);
        } else {
            if (position == mItems.size() - mLastCount) {
                mLastCount = mItems.size();
            }

            ImageLoader.getInstance().displayImage(imageUrl, holder.news_img, mOptions);
        }


        holder.ripple_view.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {

            @Override
            public void onComplete(RippleView rippleView) {
                Intent intent = new Intent(mContext, NewsDetailActivity.class);

                Bundle bundle = new Bundle();
                bundle.putString("id", mItems.get(position).id);
                intent.putExtras(bundle);

                mContext.startActivity(intent);
            }

        });



        SpannableStringBuilder builder = new SpannableStringBuilder("网易@擦肩而过：" + mItems.get(position).title);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.main_color));
        builder.setSpan(redSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        holder.news_comment.setText(builder);
        holder.news_title.setText(mItems.get(position).title);

        return convertView;
    }

    public void addItems(ArrayList<NewsBean> items, int pager) {

        if (pager == 0) {
            mItems.clear();
        }

        mLastCount = items.size();
        mItems.addAll(items);

    }

    @Override
    public NewsBean getItem(int position) {
        return mItems.get(position);
    }

    public ArrayList<NewsBean> getItems() {
        return mItems;
    }


    public void clearItems() {
        mItems.clear();
    }

    public void updateItem(int position, NewsBean item) {

        mItems.set(position, item);

    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }


    public class ViewHolder {
        public TextView news_title;
        public TextView news_comment;
        public ImageView news_img;
        public RippleView ripple_view;
    }

}
